using System;
using System.Drawing;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using System.Data;
using Microsoft.ApplicationBlocks.Data;
using Dimok.User;
using Dimok.Data;
using Dimok.Data.Database;

namespace SendGLs {
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form {
		private System.Windows.Forms.TextBox txtPath;
		private System.Windows.Forms.FolderBrowserDialog dlgPath;
		private System.Windows.Forms.Button btnGetPath;
		private System.Windows.Forms.StatusBar statusBar1;
		private System.Windows.Forms.StatusBarPanel stpState;
		private System.Windows.Forms.StatusBarPanel stpProgress;
		private System.Windows.Forms.Button btnGO;
		private System.Windows.Forms.TextBox txtStart;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox txtErrors;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public MainForm() {
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing ) {
			if( disposing ) {
				if (components != null) {
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent() {
			this.txtPath = new System.Windows.Forms.TextBox();
			this.dlgPath = new System.Windows.Forms.FolderBrowserDialog();
			this.btnGetPath = new System.Windows.Forms.Button();
			this.statusBar1 = new System.Windows.Forms.StatusBar();
			this.stpState = new System.Windows.Forms.StatusBarPanel();
			this.stpProgress = new System.Windows.Forms.StatusBarPanel();
			this.btnGO = new System.Windows.Forms.Button();
			this.txtStart = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.txtErrors = new System.Windows.Forms.TextBox();
			((System.ComponentModel.ISupportInitialize)(this.stpState)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.stpProgress)).BeginInit();
			this.SuspendLayout();
			// 
			// txtPath
			// 
			this.txtPath.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.txtPath.Location = new System.Drawing.Point(0, 0);
			this.txtPath.Name = "txtPath";
			this.txtPath.Size = new System.Drawing.Size(536, 22);
			this.txtPath.TabIndex = 0;
			this.txtPath.Text = "";
			// 
			// dlgPath
			// 
			this.dlgPath.ShowNewFolderButton = false;
			// 
			// btnGetPath
			// 
			this.btnGetPath.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.btnGetPath.Location = new System.Drawing.Point(544, 0);
			this.btnGetPath.Name = "btnGetPath";
			this.btnGetPath.Size = new System.Drawing.Size(32, 24);
			this.btnGetPath.TabIndex = 1;
			this.btnGetPath.Text = "...";
			this.btnGetPath.Click += new System.EventHandler(this.btnGetPath_Click);
			// 
			// statusBar1
			// 
			this.statusBar1.Location = new System.Drawing.Point(0, 414);
			this.statusBar1.Name = "statusBar1";
			this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
																																									this.stpState,
																																									this.stpProgress});
			this.statusBar1.ShowPanels = true;
			this.statusBar1.Size = new System.Drawing.Size(576, 22);
			this.statusBar1.TabIndex = 2;
			// 
			// stpState
			// 
			this.stpState.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Contents;
			this.stpState.Text = "State";
			this.stpState.Width = 46;
			// 
			// stpProgress
			// 
			this.stpProgress.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
			this.stpProgress.Text = "Progress";
			this.stpProgress.Width = 514;
			// 
			// btnGO
			// 
			this.btnGO.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.btnGO.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnGO.Location = new System.Drawing.Point(488, 32);
			this.btnGO.Name = "btnGO";
			this.btnGO.Size = new System.Drawing.Size(88, 24);
			this.btnGO.TabIndex = 3;
			this.btnGO.Text = "GO!";
			this.btnGO.Click += new System.EventHandler(this.btnGO_Click);
			// 
			// txtStart
			// 
			this.txtStart.Location = new System.Drawing.Point(40, 24);
			this.txtStart.Name = "txtStart";
			this.txtStart.Size = new System.Drawing.Size(48, 22);
			this.txtStart.TabIndex = 4;
			this.txtStart.Text = "1";
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.Location = new System.Drawing.Point(0, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(34, 18);
			this.label1.TabIndex = 5;
			this.label1.Text = "Start";
			// 
			// txtErrors
			// 
			this.txtErrors.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.txtErrors.Location = new System.Drawing.Point(0, 64);
			this.txtErrors.Multiline = true;
			this.txtErrors.Name = "txtErrors";
			this.txtErrors.ScrollBars = System.Windows.Forms.ScrollBars.Both;
			this.txtErrors.Size = new System.Drawing.Size(576, 352);
			this.txtErrors.TabIndex = 6;
			this.txtErrors.Text = "";
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(7, 15);
			this.ClientSize = new System.Drawing.Size(576, 436);
			this.Controls.Add(this.txtErrors);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.txtStart);
			this.Controls.Add(this.btnGO);
			this.Controls.Add(this.statusBar1);
			this.Controls.Add(this.btnGetPath);
			this.Controls.Add(this.txtPath);
			this.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Name = "MainForm";
			this.Text = "Form1";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.MainForm_Closing);
			((System.ComponentModel.ISupportInitialize)(this.stpState)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.stpProgress)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() {
			Application.Run(new MainForm());
		}

		private void btnGetPath_Click(object sender, System.EventArgs e) {
			dlgPath.ShowDialog();
			txtPath.Text = dlgPath.SelectedPath;
		}
		private bool GoToHell = false;
		private int Start{get{return int.Parse(txtStart.Text);}}
		private void btnGO_Click(object sender, System.EventArgs e){
			string adoConn = UserObject.dbRawAdo;
			string strInProc = SQLHelperEx.ADOToString(adoConn,"sec_List_InProcess");
			DataSet dsInProc = SqlHelper.ExecuteDataset(UserObject.dbRaw,CommandType.Text,"SELECT * FROM vsec_List_InProcess");
			string sql = @"
SELECT t_CarInfo.DealerID, MAX(t_Applicants.DateStarted) AS DateStarted, COUNT(*) AS [Count], t_Dealer.Name, t_Dealer.FAX
FROM  t_Applicants INNER JOIN
               t_CarInfo ON t_Applicants.ApplicantID = t_CarInfo.ApplicantID INNER JOIN
               t_Dealer ON t_CarInfo.DealerID = t_Dealer.DealerID
GROUP BY t_CarInfo.DealerID, t_Dealer.Name, t_Dealer.FAX
HAVING (MAX(t_Applicants.DateStarted) > DATEADD(mm, - 3, GETDATE()))
ORDER BY COUNT(*) DESC";
			DataRowCollection drcDealers = SQLHelperEx.ExecuteDataset(UserObject.dbRaw,sql).Tables[0].Rows;
			string path = Path.Combine(Directory.GetCurrentDirectory(),"Dimok.doc");
			for( int i=0; i < drcDealers.Count && !GoToHell; i++ ){
				try{
					if( i < Start-1 )continue;
					DataRow row = drcDealers[i];
					string fax = row["Fax"].ToString();
					if( fax.Length < 10 )continue;
					stpProgress.Text = (i+1)+"/"+drcDealers.Count;
					Application.DoEvents();
					StringCollection scFiles = new StringCollection();
					scFiles.Add(path);
                    throw new NotImplementedException();
					//Dimok.Fax.HTML("",row["Name"]+"/"+row["Count"],"Help",row["Fax"].ToString(),"AFCFAX@AFCHOME.NET","afcmail",null,scFiles);
				}catch(Exception exc){
					txtErrors.Text +="****** "+DateTime.Now+" ******"+Environment.NewLine+Dimok.Exceptions.ClientException.GetMessages(exc);
				}
			}
		}
		private void btnGO_Click_GL(object sender, System.EventArgs e){
			DataRowCollection drcDealers = SQLHelperEx.ExecuteDataset(UserObject.dbRaw,"SELECT Name, State, ISNULL(FAX,'') Fax FROM  dbo.t_Dealer ORDER BY State,Name").Tables[0].Rows;
			string path = Path.Combine(txtPath.Text,"Programs-");
			for( int i=0; i < drcDealers.Count && !GoToHell; i++ ){
				try{
					if( i < Start-1 )continue;
					DataRow row = drcDealers[i];
					string state = row["State"].ToString();
					string fax = row["Fax"].ToString();
					if( fax.Length < 10 )continue;
					stpState.Text = state;
					stpProgress.Text = (i+1)+"/"+drcDealers.Count;
					Application.DoEvents();
					StringCollection scFiles = new StringCollection();
					scFiles.Add(Path.Combine(dlgPath.SelectedPath,"Header.doc"));
					if( !File.Exists(scFiles[0]) )continue;
					scFiles.Add(path+row["State"]+".doc");
					if( !File.Exists(scFiles[1]) )continue;
					scFiles.Add(path+row["State"]+"BK.doc");
					if( !File.Exists(scFiles[2]) )continue;
                    throw new NotImplementedException();
                    //Dimok.Common.FaxHTML("",row["Name"]+"/"+row["State"],"Help",row["Fax"].ToString(),"AFCFAX@AFCHOME.NET","afcmail",null,scFiles);
				}catch(Exception exc){
					txtErrors.Text +="****** "+DateTime.Now+" ******"+Environment.NewLine+Dimok.Exceptions.ClientException.GetMessages(exc);
				}
			}
		}

		private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
			GoToHell = true;
		}
	}
}
